package com.gy.assistant.ui.meeting.adapter;

import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

import im.zego.zim.entity.ZIMCallInfo;

public class MeetingListAdapter extends RecyclerView.Adapter<MeetingListItemHolder> {
    private final ArrayList<ZIMCallInfo> callList = new ArrayList<>();
    private IClickListener mListener;
    @NonNull
    @Override
    public MeetingListItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MeetingListItemHolder(parent);
    }

    @Override
    public void onBindViewHolder(@NonNull MeetingListItemHolder holder, int position) {
        ZIMCallInfo data = callList.get(position);
        holder.setData(data);

        if (mListener!=null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener!=null){
                        mListener.click(data);
                    }
                }
            });
        }

    }

    public void setClickListener(IClickListener clickListener){
        this.mListener = clickListener;
    }

    public interface IClickListener{
        void click(ZIMCallInfo callInfo);
    }

    public void addDataAndNotify(ArrayList<ZIMCallInfo> data) {
        int oldPosition = Math.max(callList.size() - 1, 0);
        callList.addAll(data);
        notifyItemRangeInserted(oldPosition, data.size());
    }

    @Override
    public int getItemCount() {
        return callList.size();
    }
}
